package com.example.himalaya.views;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.himalaya.R;
import com.example.himalaya.adapters.PlayerListAdapter;
import com.example.himalaya.base.BaseApplication;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.player.service.XmPlayListControl;

import java.util.List;

public class SobPopWindow  extends PopupWindow {
    private final View mPopView;
    private View mCloseBtn;
    private RecyclerView mTrackList;
    private PlayerListAdapter mPlayerListAdapter;
    private TextView mPlayModeTv;
    private ImageView mPlayModeIv;
    private View mPlayModeContainer;
    private PlayListPlayModeClickListener mPlayListPlayModeClickListener = null;

    public SobPopWindow() {
        //设置宽高
        super(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //这里要注意：设置setOutsideTouchable之前，先要设置：setBackgroundDrawable,
        //否则点击外部无法关闭pop.
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setOutsideTouchable(true);

        //载进来View
        mPopView = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout.pop_play_list, null);
        //设置内容
        setContentView(mPopView);
        //设置窗口进入和退出的动画
        setAnimationStyle(R.style.pop_animation);
        initView();
        initEvent();
    }

    private void initView() {
        mCloseBtn = mPopView.findViewById(R.id.play_list_close_btn);
        mTrackList = mPopView.findViewById(R.id.play_list_rv);
        //这是布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(BaseApplication.getAppContext());
        mTrackList.setLayoutManager(linearLayoutManager);
        //设置适配器
        mPlayerListAdapter = new PlayerListAdapter();
        //添加适配器数据
        mTrackList.setAdapter(mPlayerListAdapter);


        mPlayModeTv = mPopView.findViewById(R.id.play_list_play_mode_tv);
        mPlayModeIv = mPopView.findViewById(R.id.play_list_play_mode_iv);
        mPlayModeContainer = mPopView.findViewById(R.id.play_list_play_mode_container);
    }

    private void initEvent() { //点击关闭按钮就可以关闭播放列表窗口
        mCloseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SobPopWindow.this.dismiss();
            }
        });

        mPlayModeContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPlayListPlayModeClickListener != null) {
                    mPlayListPlayModeClickListener.onPlayModeClick();
                }
            }
        });
    }

    //给适配器设置数据
    public void setListData(List<Track> list) {
        if (mPlayerListAdapter != null) {
            mPlayerListAdapter.setData(list);

        }
    }
    //设置播放列表里当前播放的位置
    public  void setCurrentPlayPosition(int position){
        if (mPlayerListAdapter != null) {
            mPlayerListAdapter.setCurrentPlayPosition(position);
            mTrackList.scrollToPosition(position);
        }
    }

    public  void setPlayListItemClickListener(PlayListItemClickListener listener){
        mPlayerListAdapter.setOnItemClickListener(listener);
    }

    //Pop播放列表中的播放图标更新
    public void updatePlayMode(XmPlayListControl.PlayMode currentMode) {
        updatePlayModeBtnImg(currentMode);
    }
    /**
     * 根据当前状态更新播放模式的图标
     * 1、列表播放：PLAY_MODEL_LIST（默认）
     * 2、列表循环：PLAY_MODEL_LIST_LOOP
     * 3、随机播放：PLAY_MODEL_RANDOM
     * 4、单曲循环：PLAY_MODEL_SINGLE_LOOP
     * */
    private void updatePlayModeBtnImg(XmPlayListControl.PlayMode playMode) {
        int resID = R.drawable.selector_play_mode_list_order;
        int textId = R.string.play_mode_order_text;
        switch (playMode){
            case PLAY_MODEL_LIST:
                resID = R.drawable.selector_play_mode_list_order;
                textId = R.string.play_mode_order_text;
                break;
            case PLAY_MODEL_RANDOM:
                resID = R.drawable.selector_paly_mode_random;
                textId = R.string.play_mode_random_text;
                break;
            case PLAY_MODEL_LIST_LOOP:
                resID = R.drawable.selector_paly_mode_list_order_looper;
                textId = R.string.play_mode_list_play_text;
                break;
            case PLAY_MODEL_SINGLE_LOOP:
                resID = R.drawable.selector_paly_mode_single_looper;
                textId = R.string.play_mode_single_play_text;
                break;
        }
        mPlayModeIv.setImageResource(resID);
        mPlayModeTv.setText(textId);
    }

    public  interface PlayListItemClickListener{
        void onItemClick(int position);
    }

    //播放列表中的播放模式
    public void setPlayListPlayModeClickListener(PlayListPlayModeClickListener playModeClickListener){
        mPlayListPlayModeClickListener = playModeClickListener;
    }

    public  interface  PlayListPlayModeClickListener{
        void onPlayModeClick();
    }
}
